package com.yinhai.message.implement.websocket;

import java.util.Map;
//import org.apache.shiro.SecurityUtils;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpSession;

/**
 * Created by yanglq on 2018/4/3 0003.
 */
public class ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        System.out.println("Before Handshake");
 /*       if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            HttpSession session = servletRequest.getServletRequest().getSession(false);
            if (session != null) {
                //使用userid区分WebSocketHandler，以便定向发送消息
                String userid = (String) session.getAttribute(Constants.WEBSOCKET_USERID);
                if (userid==null) {
                    userid="default-system";
                }
                attributes.put(Constants.WEBSOCKET_USERID,userid);
            }
        }*/

        //使用userid区分WebSocketHandler，以便定向发送消息(使用shiro获取session,或是使用上面的方式)
       /* String userid = (String) SecurityUtils.getSubject().getSession().getAttribute(Constants.WEBSOCKET_USERID);
        if (userid == null) {
            userid = "default-system";
        }
        attributes.put(Constants.WEBSOCKET_USERID, userid);*/

        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
        System.out.println("After Handshake");
        super.afterHandshake(request, response, wsHandler, ex);
    }

}
